package 双周赛;

import java.util.*;

/**
 *
 */
public class 第93场双周赛 {

    public static void main(String[] args) {

        System.out.println(maximumValue(new String[]{"alic3", "bob", "3", "4", "00000"}));

        System.out.println(maxStarSum(new int[]{17, 49, -34, -17, -7, -23, 24},
                new int[][]{{3, 1}, {5, 1}, {0, 3}, {4, 6}, {1, 4}, {3, 4}, {6, 3}, {2, 6}, {5, 2}, {1, 6}, {6, 0}, {2, 3}, {3, 5}, {2, 1}, {0, 2}, {5, 0}, {0, 4}},
                6));

    }

    public static int maximumValue(String[] strs) {
        int max = 0;

        for (String str : strs) {
            boolean flag = false;
            for (int i = 0; i < str.length(); i++) {
                if (!Character.isDigit(str.charAt(i))) {
                    flag = true;
                    break;
                }
            }

            if (flag) {
                max = Math.max(max, str.length());
            } else {
                int left = 0;
                while (left < str.length() && str.charAt(left) == '0') left++;

                int num = 0;
                int n = 0;
                for (int i = str.length() - 1; i >= left; i--) {
                    num += (str.charAt(i) - '0') * Math.pow(10, n);
                    n++;
                }

                max = Math.max(max, num);
            }
        }
        return max;
    }


    // 构建图+模拟
    public static int maxStarSum(int[] vals, int[][] edges, int k) {
        int max = Arrays.stream(vals).max().getAsInt();
        Map<Integer, List<Integer>> map = new HashMap<>();

        for (int[] edge : edges) {
            List<Integer> list1 = map.get(edge[0]);
            if (list1 == null) {
                list1 = new ArrayList<>();
                map.put(edge[0], list1);
            }
            List<Integer> list2 = map.get(edge[1]);
            if (list2 == null) {
                list2 = new ArrayList<>();
                map.put(edge[1], list2);
            }

            list1.add(edge[1]);
            list2.add(edge[0]);
        }

        for (Map.Entry<Integer, List<Integer>> entry : map.entrySet()) {
            List<Integer> list = entry.getValue();
            list.sort((a, b) -> vals[b] - vals[a]);
            int sum = vals[entry.getKey()];
            for (int i = 0; i < list.size() && i < k && vals[list.get(i)] > 0; i++) {
                sum += vals[list.get(i)];
            }
            max = Math.max(max, sum);
        }

        return max;
    }
}
